<?php
namespace Crm\Api;

use PhalApi\Api;
use PhalApi\Request;
use PhalApi\Exception\BadRequestException;
use Auth\Api\Token as Token;
use Crm\Domain\Log as DomainLog;

ini_set("display_errors","off");
/**
 * 跟进日志服务
 *
 * @author: luodong <751450467@qq.com> 2020-01-04
 */

class Log extends Api {

    /**
     * token对象
     */
    public $token;

    /**
     * 当前用户信息
     */
    public $user;

    public function __construct()
    {
        //允许所有域名发起的跨域请求
        header('Access-Control-Allow-Origin: *');  //支持全域名访问，不安全，部署后需要固定限制为客户端网址
        //header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http 动作
        //header('Access-Control-Allow-Headers:Authorization,content-type');  //响应头 请按照自己需求添加。
        //$this->di = \PhalApi\DI();
        $this->token = new Token();

    }

    public function getRules() {
        return array(
            //添加跟进
            'addLog' => array(
                'id' => array('name' => 'id', 'type'=>'int', 'require' => true, 'desc' => '跟进主体ID'),
                'r' => array('name' => 'r', 'type'=>'string', 'require' => true, 'desc' => '主体关系表'),
                'subject' => array('name' => 'subject', 'type'=>'string', 'require' => true, 'desc' => '主题'),
                'content' => array('name' => 'content', 'type'=>'string', 'require' => true, 'desc' => '内容')
            )
        );

    }

    /**
     * 获取搜索参数
     * @desc 获取数定义
     * @return obj 参数信息
     */
    public function getParams(){
        $domain = new DomainLog();
        $params['subjectOption'] =$domain->subject_option;
        $params['statusData'] = $domain->wi_option;
        return $params;
    }

    /**
     * 跟进
     * @desc 添加跟进
     * @return int id 新增的ID
     * @return int time 当前时间戳
     */

    public function addLog() {
        $this->user = $this->token->checkToken();

        error_log("addCustomerLog!", 0);
        //插入主表数据
        $domain = new DomainLog();
        $rlt = $domain->addLog($this->user);
        if($rlt['status']=='no'){
            throw new BadRequestException($rlt['msg'], 1);
            exit;
        }else{
            return $rlt['id'];
        }
    }

    /**
     * 更新意向用户
     * @desc 添加意向客户信息(客户姓名、性别、手机、资金需求量、客户类型(资质)、客户来源、地区等)
     * @return int id 新增的ID
     * @return int time 当前时间戳
     */
    public function update(){
        $this->user = $this->token->checkToken();
        error_log("updateCustomer!", 0);
        //主表数据
        $params =  \PhalApi\DI()->request->getAll();
        //插入主表数据
        $domain = new DomainCustomer();
        $id = $domain->update($params,$this->user["user_id"]);

        return $params["customer_id"];

    }


    /**
     * 获取一条日志详情

     * @return array data 客户信息
     * @return int time 当前时间戳
     */
    public function detail()
    {
        $this->user = $this->token->checkToken();
        error_log("DetailVisit!", 0);
        //主表数据
        $params =  \PhalApi\DI()->request->getAll();
        //插入主表数据
        $domain = new DomainCustomer();
        $data = $domain->detailVisit($params["visit_id"],$this->user["user_id"]);
        return $data;
    }

}
